﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Microsoft.Dynamics.GP.eConnect.Serialization;
using System.Xml.Serialization;
using Microsoft.Dynamics.GP.eConnect;
using System.Xml;
using System.IO;
using System.Linq;
using System.Web.Services;

using System.Text;
using System.EnterpriseServices;

namespace ws
{

    [WebService(Namespace = "http://csupomona.edu/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class SalesDoc : System.Web.Services.WebService
    {

        [WebMethod]
        public string Send()
        {
            string connectionString = "Data Source=localhost;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TWO;";
            eConnectMethods eConCall = new eConnectMethods();
            bool status = false;

            try
            {
                string xmlPath = HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data");
                xmlPath += "\\" + "SalesDocList.xml";
                XmlDocument xd = new XmlDocument();
                xd.Load(xmlPath);
                string salesDocDocument = xd.OuterXml;

                status = eConCall.eConnect_EntryPoint(connectionString,
                                                      EnumTypes.ConnectionStringType.SqlClient,
                                                      salesDocDocument,
                                                      EnumTypes.SchemaValidationType.None);

            }
            catch (eConnectException ece)
            {
                Console.Write(ece.ToString());
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
            finally
            {
                eConCall.Dispose();
            }
            Console.Write(status);
            return string.Empty;
        }

        [WebMethod]
        public string Receive()
        {
            eConnectType ect = new eConnectType();
            XmlSerializer serializer = new XmlSerializer(ect.GetType());
            eConnectType eConnect = new eConnectType();
            RQeConnectOutType eConnectouttype = new RQeConnectOutType();
            eConnectOut eConnectOut = new eConnectOut();
            eConnectMethods entrypoint = new eConnectMethods();
            string requesterDoc = null;
            string sConnectionString = null;
            try
            {
                //Populate the eConnectOut object to specify the data request
                eConnectOut.DOCTYPE = "Customer";
                eConnectOut.OUTPUTTYPE = 1;
                eConnectOut.INDEX1FROM = "AARONFIT0001";
                eConnectOut.INDEX1TO = "AARONFIT0001";
                eConnectOut.FORLIST = 1;

                //Create an XML document for the request
                eConnectouttype.eConnectOut = eConnectOut;
                eConnect.RQeConnectOutType = new RQeConnectOutType[1] { eConnectouttype };

                //Serialize the eConnect object to a memory stream
                MemoryStream memStream = new MemoryStream();
                serializer.Serialize(memStream, eConnect);
                memStream.Position = 0;
                //Use the memory stream to load an Xml document
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(memStream);
                memStream.Close();

                sConnectionString = "Data Source=localhost;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TWO;";
                //Request the data from the server
                requesterDoc = entrypoint.eConnect_Requester(sConnectionString, EnumTypes.ConnectionStringType.SqlClient, xmlDoc.OuterXml);
                //Write the customer XML to a file

                string xmlPath = HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data");
                xmlPath += "\\" + "Customer.xml";

                FileStream fs = new FileStream(xmlPath, FileMode.Create);
                StreamWriter writer = new StreamWriter(fs, new UTF8Encoding());
                writer.Write(requesterDoc);
                writer.Close();
            }
            catch (eConnectException exp)
            {
                Console.Write(exp.ToString());
            }
            catch (System.Exception ex)
            {
                Console.Write(ex.ToString());
            }

            return string.Empty;
        }
    }
}